﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BatchFolderCreate
{
    public partial class frmMain : Form
    {
        bool createfiles = false;
        int count = 0;
        int endnum = 0;
        public frmMain()
        {
            InitializeComponent();
            txtPre.Enabled = false; //Disable both the prefix/suffix boxes by default (for code sanity more than anything else!)
            txtSuf.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void cmdSetStart_Click(object sender, EventArgs e)
        {
            fldrSet.ShowDialog(); //Show the folder select dialog.
            try //Just in case the user cancels the directory select box.
            {
                Directory.SetCurrentDirectory(fldrSet.SelectedPath); //Set the working directory to the one the user chose, this one first to avoid changing the label to null.
                lblCurPath.Text = fldrSet.SelectedPath; //Show the selected path.
            }
            catch (System.ArgumentException)
            {
                return;
            }
        }

        private void chkEnablePre_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEnablePre.Checked) //Simple enough, enable the prefix box if the checkbox is checked.
                txtPre.Enabled = true;
            else
                txtPre.Enabled = false;

        }

        private void chkEnableSuf_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEnableSuf.Checked) //Same as for the prefix box, but targeting the suffix box.
                txtSuf.Enabled = true;
            else
                txtSuf.Enabled = false;
        }

        private void cmdStart_Click(object sender, EventArgs e)
        {

            if (fldrSet.SelectedPath == "") //Best to check this, rather than simply dumping the objects in the program's folder.
            {
                MessageBox.Show("Please select a starting folder.", "Error");
            }
            else
            {
                try //Just in case the user enters non-integer data for the start/end numbers, best to check.
                {
                    count = Convert.ToInt32(txtStartNum.Text);
                    endnum = Convert.ToInt32(txtEndNum.Text);
                }
                catch (System.FormatException) //Catch the exception, berate the user and return.
                {
                    MessageBox.Show("Please enter integers only for the start and end numbers.", "Error");
                    return;
                }
                while (count <= endnum) //Continue creating objects in increments until the 'count' (of objects) equals the 'endnum' or figure to stop at.
                {
                    if (createfiles != true) //If we're creating folders
                    {
                        System.IO.Directory.CreateDirectory(txtPre.Text + count + txtSuf.Text); //Create the folder including the prefix/suffix if any and the number, increase the count and back to the start.
                        count += 1;
                    }
                    else //If we're creating files
                    {
                        System.IO.File.Create(txtPre.Text + count + txtSuf.Text + '.' + txtExt.Text); //Same as for folders, but creating files and adding the extension.
                        count += 1;
                    }
                }
                if (createfiles != true) //Show the appropriate message box (depending on whether files or folders were created)
                    MessageBox.Show("Operation Complete\r\n" + (count - Convert.ToInt32(txtStartNum.Text)) + " folders created.", "Complete");
                else
                    MessageBox.Show("Operation Complete\r\n" + (count - Convert.ToInt32(txtStartNum.Text)) + " " + txtExt.Text + " files created.", "Complete");
            }
         }

        private void chkFiles_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFiles.Checked) //Show/hide the file extension stuff and flick the 'create files' bool switch on and off depending on whether the checkbox is checked.
            {
                lblFileExt.Visible = true;
                txtExt.Visible = true;
                createfiles = true;
            }
            else
            {
                lblFileExt.Visible = false;
                txtExt.Visible = false;
                createfiles = false;
            }
        }

    }
}
